﻿using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace LpbPrj.Client.Controls
{
    public class CirclePanelEx : CirclePanel
    {
        public static readonly DependencyProperty DatasProperty = DependencyProperty.Register("Datas", typeof(List<string>), typeof(CirclePanelEx), new FrameworkPropertyMetadata(new List<string>(), FrameworkPropertyMetadataOptions.AffectsMeasure));

        public List<string> Datas
        {
            get => (List<string>)GetValue(DatasProperty);
            set => SetValue(DatasProperty, value);
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            if (base.Children.Count == 0) return finalSize;
            if (this.Datas.Count == 0) return finalSize;

            bool keepVertical = KeepVertical;
            double offsetAngle = OffsetAngle;
            UIElement childBase = base.Children[0];
            int num = 0;
            double num2 = 360.0 / (double)base.Children.Count;
            double num3 = Diameter / 2.0;

            for (int i = 0; i < this.Datas.Count; i++)
            {
                UIElement child = null;
                {
                    if (childBase is Label input)
                    {
                        child = new Label
                        {
                            Style = input.Style,
                            Content = this.Datas[i],
                        };
                    }
                }
                {
                    if (childBase is TextBlock input)
                    {
                        child = new TextBlock
                        {
                            Style = input.Style,
                            Text = this.Datas[i],
                        };
                    }
                }
                {
                    if (childBase is Line input)
                    {
                        child = new Line
                        {
                            X1 = input.X1,
                            X2 = input.X2,
                            Y1 = input.Y1,
                            Y2 = input.Y2,
                            StrokeThickness = input.StrokeThickness,
                            Stroke = input.Stroke,
                        };
                    }
                }

                double num4 = child.DesiredSize.Width / 2.0;
                double num5 = child.DesiredSize.Height / 2.0;
                double num6 = num2 * (double)num++ + offsetAngle;
                RotateTransform rotateTransform = (RotateTransform)(child.RenderTransform = new RotateTransform
                {
                    CenterX = num4,
                    CenterY = num5,
                    Angle = (keepVertical ? 0.0 : num6)
                });
                double num7 = Math.PI * num6 / 180.0;
                double num8 = num3 * Math.Cos(num7);
                double num9 = num3 * Math.Sin(num7);
                double x = num8 + finalSize.Width / 2.0 - num4;
                double y = num9 + finalSize.Height / 2.0 - num5;
                child.Arrange(new Rect(x, y, child.DesiredSize.Width, child.DesiredSize.Height));

                base.Children.Add(child);
            }

            return finalSize;
        }
    }
}
